/*
 * support3
 */
package com.pitop.errorcode.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.enums.LanguageEnum;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.errorcode.entity.ErrorcodeLocal;
import com.pitop.errorcode.entity.ErrorcodePool;
import com.pitop.errorcode.service.ErrorcodeLocalSV;
import com.pitop.errorcode.service.ErrorcodePoolSV;
import com.pitop.errorcode.vo.ErrorcodeLocalVO;
import com.pitop.scene.entity.Scene;
import com.pitop.scene.service.SceneSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 错误码本地化，将对应国际化进行配置 控制器
 *
 * @author support3
 */
@RestController
@RequestMapping("/errorcodeLocal")
@Slf4j
@Api(value = "错误码本地化，将对应国际化进行配置控制器", description = "错误码本地化，将对应国际化进行配置控制器")
public class ErrorcodeLocalCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ErrorcodeLocalSV errorcodeLocalSV;

    @Resource
    private ErrorcodePoolSV errorcodePoolSV;


    @Resource
    private SceneSV sceneSV;

    /**
     * 查询ErrorcodeLocal一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ErrorcodeLocal一个详情信息", notes = "查询ErrorcodeLocal一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (code == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        ErrorcodeLocal errorcodeLocal = errorcodeLocalSV.load(id, code);
        log.info(JSON.toJSONString(errorcodeLocal));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodeLocal);
    }

    /**
     * 根据场景编码、错误编码、语言查询错误信息数据
     *
     * @param errorcodeLocal
     * @return
     */
    @ApiOperation(value = "根据场景编码、错误编码、语言查询错误信息数据", notes = "根据场景编码、错误编码、语言查询错误信息数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "errorCode", value = "错误编码", dataType = "java.lang.String", paramType = "path", required = true),
            @ApiImplicitParam(name = "language", value = "语言", dataType = "java.lang.String", paramType = "path", required = true)
    })
    @GetMapping(value = "/loadByParam/{errorCode}/{language}")
    @ResponseBody
    public BeanRet loadByParam(@ApiIgnore ErrorcodeLocal errorcodeLocal) {
        if (StringUtils.isBlank(errorcodeLocal.getErrorCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isBlank(errorcodeLocal.getLanguage())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        ErrorcodeLocal resultLocal = errorcodeLocalSV.loadByScodeAndEcodeAndLanguage(errorcodeLocal);
        if (resultLocal == null) {
            resultLocal = new ErrorcodeLocal();
            resultLocal.setErrorCode(BaseException.BaseExceptionEnum.Server_Error.errCode);
            resultLocal.setInfo(BaseException.BaseExceptionEnum.Server_Error.error);
        }
        log.info(JSON.toJSONString(resultLocal));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, resultLocal);
    }


    /**
     * 查询ErrorcodeLocal信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ErrorcodeLocal信息集合", notes = "查询ErrorcodeLocal信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sceneCode", value = "场景名", paramType = "query"),
            @ApiImplicitParam(name = "errorCode", value = "错误码信息", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ErrorcodeLocal errorcodeLocal, Integer curPage, Integer pageSize) {
        Page<ErrorcodeLocal> page = new Page<>(pageSize, curPage);
        if (StringTools.isEmpty(errorcodeLocal.getLanguage())) {
            errorcodeLocal.setLanguage(LanguageEnum.zh_CN.name());
        }
        List<ErrorcodeLocal> errorcodeLocals = errorcodeLocalSV.list(errorcodeLocal, page.genRowStart(), page.getPageSize());
        List<ErrorcodeLocal> errorcodeLocalList;
        for (ErrorcodeLocal errorcodelocalFlag : errorcodeLocals) {
            errorcodeLocalList = errorcodeLocalSV.listByErrorCode(errorcodelocalFlag.getErrorCode(), errorcodelocalFlag.getLanguage());
            errorcodelocalFlag.setErrorcodeLocalVOS(StringTools.convertList(errorcodeLocalList, ErrorcodeLocalVO.class));
        }
        page.setVoList(errorcodeLocals);
        int total = errorcodeLocalSV.count(errorcodeLocal);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, page);
    }


    /**
     * 创建ErrorcodeLocal
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ErrorcodeLocal", notes = "创建ErrorcodeLocal")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneCode", value = "场景编码", paramType = "query"),
            @ApiImplicitParam(name = "errorCode", value = "错误码", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言(1014):如 英语，普通话，法语等", paramType = "query"),
            @ApiImplicitParam(name = "info", value = "文本对照 中文，英语，法语等等具体的文本", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@RequestBody ErrorcodeLocal errorcodeLocal) {
        if (StringUtils.isBlank(errorcodeLocal.getSceneCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isBlank(errorcodeLocal.getErrorCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        //错误码是否存在
        ErrorcodePool errorcodePool = errorcodePoolSV.loadByErrorCode(errorcodeLocal.getErrorCode());
        if (errorcodePool == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //场景码是否存在
        Scene scene = sceneSV.loadByCode(errorcodeLocal.getSceneCode());
        if (scene == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        errorcodeLocalSV.save(errorcodeLocal);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodeLocal);
    }


    /**
     * 修改错误码本地化
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改错误码本地化", notes = "修改错误码本地化")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "sceneCode", value = "场景编码", paramType = "query"),
            @ApiImplicitParam(name = "errorCode", value = "错误码", paramType = "query", required = true),
            @ApiImplicitParam(name = "language", value = "语言(1014):如 英语，普通话，法语等", paramType = "query", required = true),
            @ApiImplicitParam(name = "info", value = "文本对照 中文，英语，法语等等具体的文本", paramType = "query", required = true)
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore ErrorcodeLocal errorcodeLocal) {
        if (errorcodeLocal == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(errorcodeLocal.getCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(errorcodeLocal.getErrorCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(errorcodeLocal.getLanguage())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(errorcodeLocal.getInfo())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        //根据场景码，错误码，语言，确定数据是否已经存在
        Map<String, Object> params = new HashMap<>();
        params.put("sceneCode", errorcodeLocal.getSceneCode());
        params.put("errorCode", errorcodeLocal.getErrorCode());
        params.put("language", errorcodeLocal.getLanguage());
        ErrorcodeLocal errorcodeLocalByParam = errorcodeLocalSV.load(params);
        if (errorcodeLocalByParam != null && !errorcodeLocal.getCode().equals(errorcodeLocalByParam.getCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Exists);
        }
        //错误码是否存在
        ErrorcodePool errorcodePool = errorcodePoolSV.loadByErrorCode(errorcodeLocal.getErrorCode());
        if (errorcodePool == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //场景码是否存在
       /* Scene scene = sceneSV.loadByCode(errorcodeLocal.getSceneCode());
        if(scene == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }*/
        errorcodeLocalSV.update(errorcodeLocal);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, errorcodeLocal);
    }

}
